# -*- coding: UTF-8 -*-

from selenium.webdriver.support.ui import Select
from common.Login import Login
import selenium.webdriver
import time
import os

class caseOperation():
    
 

    def caseSelect(self,caseId):
        driver=selenium.webdriver.Firefox()
        L=Login()
        L.login(driver,0,0)
        driver.switch_to_frame('leftFrame')
        driver.find_element_by_xpath("//dd[4]/div").click()
        time.sleep(0.2)   
        driver.find_element_by_link_text(u"信访件查询").click()
        driver.switch_to_default_content()
        driver.switch_to_frame('rightFrame')
        Select(driver.find_element_by_id("searchTypeSelectOne0")).select_by_visible_text(u"信访事项")
        Select(driver.find_element_by_id("searchTypeSelectTwoTwo0")).select_by_visible_text(u"信访件编号")
        Select(driver.find_element_by_id("relationInputSelect0")).select_by_visible_text(u"包含有")
        driver.find_element_by_id("searchKey0").clear()
        driver.find_element_by_id("searchKey0").send_keys(caseId)
        driver.find_element_by_xpath("//input[@value='查询']").click()
    
    def letterRigister(self,):
        L=Login()
        DictSet = L.login(0,0)
        driver = DictSet["driver"]
        userName = DictSet["userName"]
        driver.implicitly_wait(30)
        print "来信登记开始，用户名："+userName+"."
        main_handle=driver.current_window_handle
        driver.maximize_window()
        driver.switch_to_frame('leftFrame')
        driver.find_element_by_link_text(u'待办理').click()
        driver.switch_to_default_content()
        driver.switch_to_frame('rightFrame')
        try:
            driver.find_element_by_xpath("//input[@value='来信登记']").click()
        except:
            print "无法找到来信登记，请尝试清除偏好，设置登记类型"
        
        allhandle=driver.window_handles
        for handle in allhandle:
            if handle != main_handle:
                driver.switch_to_window(handle)
                letter_handle=driver.current_window_handle
                assert u'来信'  in driver.title
                driver.find_element_by_id('sbjName').clear()
                driver.find_element_by_id('sbjName').send_keys(u'测试123')
                driver.find_element_by_id("multiSelect").click()
                driver.find_element_by_link_text(u"四川省").click()
                driver.find_element_by_link_text(u"内江市").click()
                driver.find_element_by_link_text(u"市中区").click()
                driver.find_element_by_css_selector("div.close").click()
                driver.find_element_by_id("judgement").click()  
                allhandles=driver.window_handles
                for handle in allhandles:
                    if handle != main_handle:
                        if handle != letter_handle:
                            driver.switch_to_window(handle)                            
                            #assert u'判重' in driver.title
                            driver.close()     
                            driver.switch_to_window(letter_handle)       
                            Select(driver.find_element_by_id('sbjSex')).select_by_visible_text(u'女')
                            Select(driver.find_element_by_id("sbjCredentialsType")).select_by_visible_text(u"居民身份证")
                            driver.find_element_by_id('sbjCredentialsNum').clear()
                            driver.find_element_by_id('sbjCredentialsNum').send_keys('510726196565656895')
                            driver.find_element_by_id('petitionDefendantName').clear()
                            driver.find_element_by_id('petitionDefendantName').send_keys('2222')
                            Select(driver.find_element_by_id('matterBelongSystemCode')).select_by_visible_text(u"军队武警")
                            driver.find_element_by_id('belongAreaInput').click()
                            driver.find_element_by_css_selector('#stock_province_item3 > ul.area-list > li > a').click()
                            driver.find_element_by_css_selector('#stock_city_item3  > ul.area-list > li > a').click()
                            driver.find_element_by_css_selector("#belongArea-store-selector > div.close").click()
                            driver.find_element_by_id('cateMultiSelect').click()
                            try:
                                driver.find_element_by_link_text(u"农村农业").click()
                            except:
                                driver.find_element_by_id('cateMultiSelect').click()
                                driver.find_element_by_link_text(u"农村农业").click()
                                driver.find_element_by_link_text(u"村务管理").click()
                                driver.find_element_by_link_text(u"集体资产管理").click()
                                
                            driver.find_element_by_link_text(u"村务管理").click()
                            driver.find_element_by_link_text(u"集体资产管理").click()    
                            driver.find_element_by_css_selector("option[value=\"01\"]").click()
                            Select(driver.find_element_by_id("petitionPurposeCode")).select_by_visible_text(u"申诉")
                            driver.find_element_by_css_selector("option[value=\"02\"]").click()
                            Select(driver.find_element_by_id("petitionTypeCode")).select_by_visible_text(u"传真")
                            driver.find_element_by_id("FollowPersons").clear()
                            driver.find_element_by_id("FollowPersons").send_keys("321312")
                            driver.find_element_by_xpath("(//input[@name='dcdbFlag'])[2]").click()
                            Select(driver.find_element_by_id("hotIssuesCode")).select_by_visible_text(u"农民工工资")
                            Select(driver.find_element_by_id("petitionReasonCode")).select_by_visible_text(u"干部作风")
                            driver.find_element_by_id("petitionContent").clear()
                            driver.find_element_by_id("petitionContent").send_keys("321312")
                            driver.find_element_by_id("petitionRemark").clear()
                            driver.find_element_by_id("petitionRemark").send_keys("321312321")
                            driver.find_element_by_id("registerForm").click()
                            driver.find_element_by_id("tipOk").click()
                            print '来信登记成功' 
                            try:
                                driver.switch_to_window(main_handle)
                                assert u'信息管理' in driver.title
                                driver.quit()
                            except:
                                print('没有正常关闭来信登记窗口')    
                            
    def assgin(self,caseId,department,nameLocation,passwordLocation):  #交办
        caseId = caseId
        department = department
        L=Login()        
        DictSet =L.login(nameLocation,passwordLocation)
        driver = DictSet["driver"]
        userName = DictSet["userName"]
        main_handle = driver.current_window_handle
#        print "开始对案件：" +caseId+ "进行交办。" + "交办人为：" + userName +"。"
        
        driver.switch_to_frame('leftFrame') 
        driver.find_element_by_link_text(u"待办理").click()
        driver.find_element_by_link_text(u"待办理").click()
        driver.find_element_by_link_text(u"待办理").click()
        driver.find_element_by_link_text(u"待办理").click()     
        driver.switch_to_default_content()
        driver.switch_to_frame("rightFrame")
        driver.find_element_by_link_text("案件编号").click()
        driver.find_element_by_link_text(caseId).click()
        try:
            driver.find_element_by_id("getAssign").click()
            print "案件为" + caseId+ "的交办已经被"+ "用户" +userName+ "接受"
        except:
            pass
        Select(driver.find_element_by_id("handleMethod")).select_by_visible_text(u"交办")
        driver.find_element_by_id("assignDesDep").clear()
        driver.find_element_by_id("assignDesDep").send_keys(department)
        driver.find_element_by_css_selector("em").click()
        driver.find_element_by_xpath(u"//input[@value='办理']").click()
        allhandles = driver.window_handles
        for handle in allhandles:
            if handle != main_handle:
                driver.switch_to_window(handle)
                try:        
                    driver.find_element_by_id("assignOpinion").send_keys(u"这是交办" + str(time.localtime()))
                    driver.find_element_by_id("assignSubmit").click()
                    driver.find_element_by_id("tipOk").click()
                except:
                    print "ERROR:没有交办成功"   

    def forward(self,caseId,department,nameLocation,passwordLocation):      #转办
        caseId = caseId
        department = department
        L=Login()        
        DictSet =L.login(nameLocation,passwordLocation)
        driver = DictSet["driver"]
        userName = DictSet["userName"]
        main_handle = driver.current_window_handle
        
        driver.switch_to_frame('leftFrame') 
        driver.find_element_by_link_text(u"待办理").click()
        driver.find_element_by_link_text(u"待办理").click()     
        driver.switch_to_default_content()
        driver.switch_to_frame("rightFrame")
        driver.find_element_by_link_text("案件编号").click()
        driver.find_element_by_link_text(caseId).click()
        try:
            driver.find_element_by_id("agreeForward").click()
            print "案件为" + caseId+"的交办已经被"+"用户"+userName+"接受"
        except:
            pass
        Select(driver.find_element_by_id("handleMethod")).select_by_visible_text(u"转办")
        driver.find_element_by_id("depNameText").clear()
        driver.find_element_by_id("depNameText").send_keys(department)
        driver.find_element_by_css_selector("em").click()
        driver.find_element_by_xpath(u"//input[@value='办理']").click()
        allhandles = driver.window_handles
        for handle in allhandles:
            if handle != main_handle:
                driver.switch_to_window(handle)
                try:        
                    driver.find_element_by_id("opinion").send_keys(u"这是转办" + str(time.localtime()))
                    driver.find_element_by_xpath(u"//input[@value='提交']").click()
                    driver.find_element_by_id("tipOk").click()
                except:
                    pass   
                
    def acceptCase(self,caseId,nameLocation,passwordLocation):       #接受案件
        caseId = caseId
        L=Login()
        DictSet =L.login(nameLocation,passwordLocation)
        driver = DictSet["driver"]
        userName = DictSet["userName"]
        driver.switch_to_frame('leftFrame')
        driver.find_element_by_link_text(u"待办理").click()
        driver.find_element_by_link_text(u"待办理").click()
        driver.switch_to_default_content()
        driver.switch_to_frame("rightFrame")
#         driver.find_element_by_link_text("案件编号").click()
        driver.find_element_by_link_text(caseId).click()
        try:
            driver.find_element_by_id("getAssign").click()
            print "案件为" + caseId+"的交办已经被"+"用户"+userName+"接受"   
        except:
            pass
        try:
            driver.find_element_by_id("agreeForward").click()
            print "案件为" + caseId+"的交办已经被"+"用户"+userName+"接受"
        except:
            pass         
        
        return driver
    
    def substantiveHandle(self,caseId,nameLocation,passwordLocation):      #实体性受理出具，包含上传附件
        L = Login()
        DictSet = L.login(nameLocation, passwordLocation)
        driver = DictSet["driver"]
        userName = DictSet["userName"]
        caseId =caseId
        main_handle = driver.current_window_handle
        try:
            driver.switch_to_frame('leftFrame') 
            driver.find_element_by_link_text(u"待办理").click()
            driver.find_element_by_link_text(u"待办理").click()
            driver.find_element_by_link_text(u"待办理").click()
            driver.find_element_by_link_text(u"待办理").click()     
            driver.switch_to_default_content()
            driver.switch_to_frame("rightFrame")
            driver.find_element_by_link_text("案件编号").click()
            driver.find_element_by_link_text(caseId).click()
            print "开始对：" + caseId+"案件出具实体性处理意见书"+"出具用户 ："+userName+"。"
            Select(driver.find_element_by_id("handleMethod")).select_by_visible_text(u"实体性受理")
            driver.find_element_by_xpath(u"//input[@value='办理']").click()
            allhandles = driver.window_handles
            for handle  in allhandles:
                if handle != main_handle:
                    driver.switch_to_window(handle)
                    caseDetail_handle = driver.current_window_handle
                    driver.find_element_by_id("petitionContent").clear()
                    driver.find_element_by_id("petitionContent").send_keys(u"这是实体性受理内容。")
                    driver.find_element_by_id("time1").click()
                    driver.find_element_by_link_text("29").click()
                    driver.find_element_by_id("uploadfile4").click()
                    driver.switch_to_alert().accept()#请上传图片附件alert
                    driver.switch_to_alert().accept()#附件还能上传几个alert
                    allhandles = driver.window_handles
                    for handle in allhandles:
                        if handle != caseDetail_handle:
                            if handle != main_handle:
                                driver.switch_to_window(handle)
                                print driver.title
                                driver.find_element_by_link_text(u'增加文件').click()
                                os.system(u"D:\\sub.exe")
                                driver.find_element_by_link_text(u'开始上传').click()    
                                driver.switch_to_alert().accept()
                                driver.switch_to_window(caseDetail_handle)
                                driver.find_element_by_xpath(u"//input[@value='否']").click()
                                print "案件：" + caseId+"出具实体性处理意见书成功"+"出具用户 ："+userName+"。"
                                driver.switch_to_window(main_handle)
                                return driver
        except:
            pass                   
                            
    #自办操作（包含上传附件），不打开driver，接受一个driver，这个driver必须可以自办。                                                      
    def selfHandle(self,caseId,nameLocation,passwordLocation):
        L = Login()
        DictSet = L.login(nameLocation, passwordLocation)
        driver = DictSet["driver"]
        userName = DictSet["userName"]
        caseId =caseId
        main_handle = driver.current_window_handle
        try:
            driver.switch_to_frame('leftFrame') 
            driver.find_element_by_link_text(u"待办理").click()
            driver.find_element_by_link_text(u"待办理").click()
            driver.find_element_by_link_text(u"待办理").click()
            driver.find_element_by_link_text(u"待办理").click()     
            driver.switch_to_default_content()
            driver.switch_to_frame("rightFrame")
            driver.find_element_by_link_text("案件编号").click()
            driver.find_element_by_link_text(caseId).click()
            print "开始对：" + caseId+"案件出具自办处理意见书。"+"出具用户 ："+userName+"。"
            Select(driver.find_element_by_id("handleMethod")).select_by_visible_text(u"自办")
        except:
            print "没有选择到自办，请确认是否出具了实体性受理"      
        driver.find_element_by_xpath(u"//input[@value='办理']").click()
        allhandles = driver.window_handles
        for handle  in allhandles:
            if handle != main_handle:
                driver.switch_to_window(handle)
                caseDetail_handle = driver.current_window_handle
                driver.find_element_by_id("reflectOpinion").clear()
                driver.find_element_by_id("reflectOpinion").send_keys(u"此为反映问题问题。") 
                driver.find_element_by_id("handleOpinion").clear()
                driver.find_element_by_id("handleOpinion").send_keys(u"此为自办处理情况问题。")
                driver.find_element_by_id("objDepartment").clear()
                driver.find_element_by_id("objDepartment").send_keys(u"申请复查单位")
                driver.find_element_by_id("sendTime").click()
                driver.find_element_by_link_text("30").click()  
                driver.find_element_by_id("sendUserName").clear()
                driver.find_element_by_id("sendUserName").send_keys(u"申请人姓名")
                driver.find_element_by_id("sendUserPhone").clear()
                driver.find_element_by_id("sendUserPhone").send_keys(u"申请人电话")
                driver.find_element_by_id("replyUnit").click()    
                driver.switch_to_alert().accept()#请上传图片附件的alert
                driver.switch_to_alert().accept()#附件还能上传几个的alert
                allhandles = driver.window_handles
                for handle in allhandles:
                    if handle != caseDetail_handle:
                        if handle != main_handle:
                            driver.switch_to_window(handle)  
                            print driver.title
                            driver.find_element_by_link_text(u'增加文件').click()
                            os.system(u"D:\\sub.exe")
                            driver.find_element_by_link_text(u'开始上传').click()    
                            driver.switch_to_alert().accept()
                            driver.switch_to_window(caseDetail_handle)
                            driver.switch_to_alert().accept()
                            time.sleep(0.5)
                            driver.find_element_by_xpath(u"//input[@value='否']").click()
                            driver.switch_to_window(main_handle)
                            print "案件：" + caseId+"出具自办处理意见书成功"+"出具用户 ："+userName+"。"
                            return driver                           
                     
        
        
    
    

        

            
                    
                      

 
        